package com.spider.silence.main;

import com.spider.silence.downloader.CRIOnlineForSiChuanDownloader;
import com.spider.silence.pool.CRIOnlineForSiChuanDownloaderPool;
import com.spider.silence.utils.PropertiesUtil;
import com.spider.silence.worker.CRIOnlineForSiChuanWorker;
import com.spider.silence.worker.ChinaNewsForSiChuanWorker;
import org.apache.log4j.PropertyConfigurator;

import java.io.IOException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by Linbo Hu on 2017/6/29.
 * 国际在线四川频道主方法
 */
public class CRIOnlineForSiChuanMain {

    public static void main(String[] args) throws Exception {
        //初始化日志环境
        PropertyConfigurator.configure(PropertiesUtil.getLogPro());
        //初始化下载池
        CRIOnlineForSiChuanDownloaderPool downloaderPool = CRIOnlineForSiChuanDownloaderPool.getInstance();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //获取一个下载器
                CRIOnlineForSiChuanDownloader downloader = null;
                try {
                    downloader = downloaderPool.fetchDownLoader(1000);
                    downloader.generateTask();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    if(downloader != null) {
                        downloaderPool.releaseDownLoader(downloader);
                    }
                }
            }
        }).start();

        Thread.sleep(5000);
        ExecutorService pool =  Executors.newFixedThreadPool(3000);
        //pool.execute(new ChinaNewsForSiChuanWorker());
        //初始化中国新闻网四川频道的Worker并且添加到线程池中
        for(int i = 0; i<Integer.valueOf(PropertiesUtil.getVal("CRIOnlineForSiChuan.workCount")); i++){
            pool.execute(new CRIOnlineForSiChuanWorker());
        }

       /* CRIOnlineForSiChuanDownloader downloader = new CRIOnlineForSiChuanDownloader();
        downloader.crawlItem("http://sc.cri.cn/20170613/7c7314c3-6007-32b8-357b-aacec73aa5e9.html");*/


    }
}
